/**
 * 定时任务执行日志
 */
layui.define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  const admin = layui.admin,
      flying = layui.flying
      ,form = layui.form
      ,table = layui.table
      ,layer = layui.layer
      ,dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-data-table',
    url: '/task/jobLog/page', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'serialNo', title: '流水号', minWidth: 100}
      , {field: 'serviceName', title: '所属服务', minWidth: 100}
      , {field: 'jobDate', title: '任务日期/批量日期', minWidth: 100}
      , {field: 'jobName', title: '任务名称', minWidth: 100}
      , {field: 'jobType', title: '任务类型  DEFAULT定时任务 BATCH批量任务', minWidth: 100}
      , {field: 'jobGroup', title: '任务分组', minWidth: 100}
      , {field: 'jobDesc', title: '任务描述', minWidth: 100}
      , {field: 'jobInstId', title: '执行实例', minWidth: 100}
      , {field: 'jobHost', title: '执行主机', minWidth: 100}
      , {field: 'jobStatus', title: '执行状态 RUNNING正在执行 SUCCESS执行成功 FAIL执行失败', minWidth: 100}
      , {field: 'duration', title: '耗时', minWidth: 100}
      , {field: 'jobMsg', title: '描述信息', minWidth: 100}
      , {field: 'startTime', title: '开始时间', templet: '<div>{{ layui.util.toDateString(d.startTime, "yyyy-MM-dd HH:mm:ss") }}</div>', minWidth: 170, }
      , {field: 'fireTime', title: '执行时间', templet: '<div>{{ layui.util.toDateString(d.fireTime, "yyyy-MM-dd HH:mm:ss") }}</div>', minWidth: 170, }
      , {field: 'endTime', title: '结束时间', templet: '<div>{{ layui.util.toDateString(d.endTime, "yyyy-MM-dd HH:mm:ss") }}</div>', minWidth: 170, }
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#op-table'}
    ]
  });

  const jobLog = {
    // 编辑
    handleEdit: function (title, id) {
      layer.open({
        type: 2
        , title: title
        , content: '/task/jobLog/turn/edit?id=' + id
        , area: ['100%', '100%']
        , resize: false
        , btn: ['确认', '取消']
        , btnAlign: 'r'
        , maxmin: true
        , yes: function (index, layero) {
          let iframeWindow = window['layui-layer-iframe' + index]
              , submitID = 'LAY-data-form-front-submit'
              , submit = layero.find('iframe').contents().find('#' + submitID);

          //监听提交
          iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
            let field = data.field; //获取提交的字段

            //提交 Ajax 成功后，静态更新表格中的数据
            admin.req({
              url: '/task/jobLog/save',
              type: 'post',
              contentType: 'application/json;charset=utf-8',
              data: JSON.stringify(field),
              success: function (res) {
                //执行成功
                layer.msg(res.msg, {icon: 1});
                table.reload('LAY-data-table'); //数据刷新
                layer.close(index); //关闭弹层
              }
            });
          });

          submit.trigger('click');
        }
      });
    },
    // 删除
    handleDelete: function (id) {
      layer.confirm('真的删除行么？', function (index) {
        //提交 Ajax 成功后，更新表格中的数据
        admin.req({
          url: '/task/jobLog/deleteById/' + id
          , type: 'get'
          , done: function (res) {
            //执行成功
            layer.msg(res.msg, {icon: 1});
            table.reload('LAY-data-table'); //数据刷新
            layer.close(index); //关闭弹层
          }
        });
      });
    }
  }

  //对外暴露的接口
  exports('jobLog', jobLog);
});